#!/usr/bin/env node

import { Command } from 'commander';
import chalk from 'chalk';
import { Transformer } from './transformer';
import { ConfigManager } from './config-manager';

const program = new Command();

program
  .name('taro-transform')
  .description('Transform WeChat miniprogram to Taro project')
  .version('1.0.0');

program
  .command('transform')
  .description('Transform miniprogram project to Taro')
  .option('-s, --source <path>', 'Source miniprogram directory', './miniprogram')
  .option('-o, --output <path>', 'Output Taro directory', './taro-project')
  .option('-c, --config <path>', 'Configuration file path', './taro-transform.config.js')
  .option('--force', 'Force overwrite existing files')
  .option('--dry-run', 'Show what would be transformed without actually doing it')
  .action(async (options) => {
    try {
      console.log(chalk.blue('🚀 Starting Taro transformation...'));
      
      const configManager = new ConfigManager(options.config);
      const config = await configManager.loadConfig();
      
      const transformer = new Transformer({
        sourcePath: options.source,
        outputPath: options.output,
        config,
        force: options.force,
        dryRun: options.dryRun
      });
      
      await transformer.transform();
      
      if (options.dryRun) {
        console.log(chalk.yellow('✅ Dry run completed. No files were actually transformed.'));
      } else {
        console.log(chalk.green('✅ Transformation completed successfully!'));
        console.log(chalk.cyan(`📁 Output directory: ${options.output}`));
      }
    } catch (error) {
      console.error(chalk.red('❌ Transformation failed:'), error);
      process.exit(1);
    }
  });

program
  .command('init')
  .description('Initialize configuration file')
  .option('-o, --output <path>', 'Output config file path', './taro-transform.config.js')
  .action(async (options) => {
    try {
      const configManager = new ConfigManager(options.output);
      await configManager.createDefaultConfig();
      console.log(chalk.green(`✅ Configuration file created: ${options.output}`));
    } catch (error) {
      console.error(chalk.red('❌ Failed to create config file:'), error);
      process.exit(1);
    }
  });

program.parse(); 